package smary.managed.session.auth;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.Serializable;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import smary.ejb.entity.Userloginaccount;
import smary.ejb.stateful.LoginServiceLocal;

/**
 * Ist das Kernstück der Session Verwaltung. Speichert pro Session einen Account
 * sowie die Login Daten. Haut ausserdem die Möglichkeit wieder auszuloggen.
 * @author Alexander Cannell
 */
@Named(value = "userSessionService")
@SessionScoped
public class UserSessionService implements Serializable {
    @Inject
    LoginServiceLocal lsl;
    
    protected Userloginaccount account;
    
    private String username;
    private String password;

    public String doLogout(){
        account = null;
        username = null;
        password = null;
        
        return "home?faces-redirect=true";
    }
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Userloginaccount getAccount() {
        
        checkLoginData();
        return account;
    }

    public void setAccount(Userloginaccount account) {
        this.account = account;
    }
    
    /** Creates a new instance of UserSessionService */
    public UserSessionService() {
    }

    private void checkLoginData() {
        System.out.println("USER:"+username);
        //System.out.println("PW:"+password);
        
        lsl.doLogin(username, password);
        account = lsl.getAccount();
        //if(username!=null && password != null)
        //account = ufl.checkLoginData(username, password);
    }
}
